<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/10/17
 * Time: 20:57
 */

namespace app\admin\controller;
use think\Db;
use app\home\model\Xuexiao;
/**
 * Class Type
 * @package app\admin\controller
 * 班型分配
 */
class Type extends Admin
{
    /**
     * 首页 
     */
    public function index(){
        $name = input('name');  // 班型名称
        $map = [];
        if ($name){
            $map['banxing'] = ['like','%'.$name.'%'];
        }
        $list = Db::table('pb_skbx')
            ->where($map)
            ->order('id','desc')
            ->paginate(10,false,['query' => ['name'=> $name]]);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 学员列表
     */
    public function two(){
        $id = input('id');  // 课程编号
        if (empty($id)){
            return $this->error('系统参数错误');
        }
        $name = input('name'); // 学校 或者 @姓名
        $party = input('party');  // 年级
        $years = date('Y');
        $Year = [0];
        while (count($Year) <= 5){
            $Year[] = $years;
            $years += 1;
        }
        $this->assign('years',$Year);
        if (empty($party)){
            $party = 0;
        }
        if ($name){
            if (substr($name,0,1) === "@"){ // 姓名搜索
                $vipp = substr($name,1);
                if ($party == 0){
                    $map = ['a.review_status' => 1,'a.role' => 0,'a.name' => ['like', "%$vipp%"]];
                }else{
                    $map = ['a.review_status' => 1,'a.role' => 0,'a.name' => ['like', "%$vipp%"],'party' => $party];
                }
            }else{
                // 学校搜索
                $dx = iconv("UTF-8", "GBK//TRANSLIT", $name);
                $School = Xuexiao::where(['dx' => $dx])->value('dx_id');
                $school_id = $School ? $School : '*';
                if ($party == 0){
                    $map = ['a.review_status' => 1,'a.role' => 0,'a.school_id' => $school_id];
                }else{
                    $map = ['a.review_status' => 1,'a.role' => 0,'a.school_id' => $school_id,'party' => $party];
                }
            }
        }else{
            if ($party == 0){
                $map = ['a.review_status' => 1,'a.role' => 0];
            }else{
                $map = ['a.review_status' => 1,'a.role' => 0,'party' => $party];
            }
        }
        $list = Db::table('pb_wechat_user')
            ->alias('a')
            ->where($map)
            ->field('a.openid,a.name,a.school_id,a.order_id')
            ->order('a.party,a.order_id','desc')
            ->paginate(10,false,['query' => ['id'=> $id,'name' => $name,'party' => $party]]);
        $this->assign('list',$list);
        $this->assign('id',$id);
        $this->assign('name',$name);
        $this->assign('party',$party);
        $this->assign('class',get_banxing($id));
        return $this->fetch();
    }
    /**
     * 批量操作
     */
    public function play(){
        $data = input('post.');
        $id = $data['id'];
        $users = $data['users']; // 数组
        $type = $data['type']; // 1 确认 2 取消
        $arr1 = [];  //已分配 id
        $arr2 = [];  //未分配
        foreach ($users as $value){
            $Sign = Db::name('class')->where(['kid' => $id,'openid' => $value])->field('id')->find();
            if ($Sign){
                // 已经分配
                array_push($arr1,$Sign['id']);
            }else{
                // 未分配
                array_push($arr2,['kid' => $id,'openid' => $value]);
            }
        }
        if ($type == 1){
            // 加入
            Db::name('class')->insertAll($arr2);
        }else{
            // 移除
            Db::name('class')->where(['id' => ['in',$arr1]])->delete();
        }
        return $this->success('操作成功');
    }
    /**
     * 该班型学员
     */
    public function three(){
        $id = input('id');  // 课程编号
        if (empty($id)){
            return $this->error('系统参数错误');
        }
        $name = input('name'); // VIP 或者 @姓名
        $party = input('party');  // 年级
        $years = date('Y');
        $Year = [0];
        while (count($Year) <= 5){
            $Year[] = $years;
            $years += 1;
        }
        $this->assign('years',$Year);
        if (empty($party)){
            $party = 0;
        }
        if (substr($name,0,1) === "@"){ // 姓名搜索
            $vipp = substr($name,1);
            if ($party == 0){
                $map = ['a.review_status' => 1,'a.role' => 0,'a.name' => ['like', "%$vipp%"]];
            }else{
                $map = ['a.review_status' => 1,'a.role' => 0,'a.name' => ['like', "%$vipp%"],'party' => $party];
            }
        }else{
            // VIP搜索
            if ($party == 0){
                $map = ['a.review_status' => 1,'a.role' => 0,'a.order_id' => ['like', '%'.$name.'%']];
            }else{
                $map = ['a.review_status' => 1,'a.role' => 0,'a.order_id' => ['like', '%'.$name.'%'],'party' => $party];
            }
        }
        $list = Db::table('pb_wechat_user')
            ->alias('a')
            ->join('pb_class b',"a.openid = b.openid")
            ->where('b.kid',$id)
            ->where($map)
            ->field('a.openid,a.name,a.school_id,a.order_id,a.tel')
            ->order('a.school_id,a.order_id','desc')
            ->paginate(10,false,['query' => ['name' => $name,'party' => $party]]);
        $sum = Db::table('pb_class')->where('kid',$id)->count();
        $this->assign('list',$list);
        $this->assign('id',$id);
        $this->assign('name',$name);
        $this->assign('party',$party);
        $this->assign('class',get_banxing($id).' — 【 该班型共有学员 __'.$sum.'__ 人 】');
        return $this->fetch();
    }
    /**
     * 删除课表
     */
//    public function del(){
//        $id = input('id');
//        if (empty($id)){
//            return $this->error('系统参数错误');
//        }
//        $res = Db::table('pb_skbx')->where('id',$id)->delete();
//        if ($res){
//            if (Db::table('pb_class')->where('kid',$id)->find()){
//                Db::table('pb_class')->where('kid',$id)->delete();
//            }
//            return $this->success('删除成功');
//        }else{
//            return $this->error('删除失败');
//        }
//    }
}